<?php
class IndexAction extends CommonAction{
	private $howtobig='';

    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        Load('extend');
        $this->header_sibar();
        $product=M('Product');
        $pdata = $this->getProduct('is_popular=1','*',4,'sortall desc');
        $this->assign('product',$pdata);
        $hdata = $product->field('think_product.*,b.big_category_name as bname')->where('think_product.is_hot=1')->join('think_bigcategory b on b.id = think_product.pid')->limit(2)->select();
        foreach($hdata as $k => $v){
        	if($v['one_download_link'] == 0){
        		$dl = M('ProductDownloadLink');
        		$pdl = $dl->where('pid='.$v['id'])->order('sort asc')->select();
        		$hdata[$k]['duoLink'] = $pdl;
        		$p = $product->field('think_product.*')->where('think_product.id='.$v['relative_product'])->find();
        		$hdata[$k]['relativePro'] = $p;
        	}else{
	        	$p = $product->field('think_product.*,b.big_category_name as bname')->join('think_bigcategory b on b.id = think_product.pid')->where('think_product.id='.$v['relative_product'])->find();
	        	$hdata[$k]['relativePro'] = $p;
        	}
        }
        $this->showbox2();
        $this->box2();
        $this->assign('hproduct',$hdata);
        $this->assign('href','show');
        $this->assign('flag',1);
		$this->assign('c_name','index');
        $this->display();

    }

    private function getAll(){
     	$b = $this->getBig();
        foreach ($b as $k => $v){
        	$pcdata = $this->getArticleClassify('pid = '.$v['id'],'*',1,'sort asc');
        	$b[$k]['acdata'] = $pcdata[0];
        	$b[$k]['pdata'] = $this->getProduct('status=1 and pid = '.$v['id'],'*','','psort asc');
        	$b[$k]['adata'] = $this->getArticle('status=1 and pid = '.$v['id']);
        }
//        echo "<pre>";var_dump($b[0]['pdata']);echo "</pre>";
        return $b;
    }
    //读取头和左侧数据
    private function header_sibar(){
    	//Load('extend');
    	$site_name = 'http://'.$_SERVER['HTTP_HOST'];
    	$this->assign('site_name',$site_name);
    	$system = $this->getSystem();
    	$this->tkd($system['webname'],$system['web_keyword'],$system['web_discrition']);
        $this->assign('system',$system);
    	$cate=$this->getAll();
    	$this->assign('cate',$cate);
    	$this->getFootLink();
    	$hotTag = $this->getHotTag();
    	$this->assign('hotTag',$hotTag);

    }
    private function getFootLink(){
    	if(file_exists('./about-us.html')){
       		$this->assign('about_us',1);
       }
		if(file_exists('./contact-us.html')){
       		$this->assign('contact_us',1);
       }
		if(file_exists('./privacy-policy.html')){
       		$this->assign('privacy_policy',1);
       }
		if(file_exists('./terms-conditions.html')){
       		$this->assign('terms_conditions',1);
       }
    	if(file_exists('./links.html')){
       		$this->assign('links',1);
       }
    	if(file_exists('./sitemap.xml')){
       		$this->assign('sitemap',1);
       }
    }
    private function getSystem(){
    	$Msystem=M("System");
        $system=$Msystem->order('id desc')->find();
        return $system;
    }
    private function getArticle($where="",$field="*",$limit = '',$order="add_time"){
    	$article=M('Article');
//        $adata=$article->join('left join think_bigcategory b on b.id=think_article.pid left join think_article_classify c on c.id=think_article.cid')->order('add_time desc')->limit(12)->select();
 		$adata=$article->field($field)->order($order.' desc')->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getHotTag($where="",$field="*",$limit = '',$order="id desc"){
    	$ht=M('HotTag');
 		$adata=$ht->field($field)->order($order)->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getArticleClassify($where="",$field="*",$limit = '',$order="id desc"){
    	$ac=M('ArticleClassify');
 		$adata=$ac->field($field)->order($order)->where($where)->limit($limit)->select();
    	return $adata;
    }
	private function getProduct($where="",$field="*",$limit = '',$order="add_time desc"){
    	$p=M('Product');
 		$pdata=$p->field($field)->where($where)->order($order)->limit($limit)->select();
    	return $pdata;
    }
	private function getBig($where="",$field="*",$limit = '',$order="add_time"){
    	$b=M('Bigcategory');
 		$cate=$b->field($field)->where($where)->order($order.' asc')->limit($limit)->select();
		foreach($cate as $k=>$v){
    		if(file_exists("./".$v['statice_name'].'-software.html')){
    			$cate[$k]['file_exit'] = 1;
    		}
    	}
    	return $cate;
    }
    //笡���级分类点击进�?
 	public function product(){
 		Load('extend');
        $this->header_sibar();
        $id=$_GET['id'];
        if(!$id){
        	return;
        }
        $bigcategory=M('Bigcategory');
        $bdata=$bigcategory->find($id);
        $this->assign('bdata',$bdata);
       $dl = M('ProductDownloadLink');
        $soft=$this->getProduct('status=1 and pid='.$id,'*','','psort asc');
        foreach($soft as $k => $v){
        	$soft[$k]['introduce'] = stripcslashes($v['introduce']);
        	if($v['one_download_link'] == 0){
        		$pdl = $dl->where('pid='.$v['id'])->order('sort asc')->select();
        		$soft[$k]['duoLink'] = $pdl;
        	}
        }
        $this->assign('soft',$soft);
        $this->tkd($bdata['big_title'],$bdata['big_keyword'],$bdata['big_description']);

        $this->assign('pid',$id);
        $this->assign('sebg',2);
        $this->display();
    }
//产品描述
   public function product_description(){
 		Load('extend');
   		$this->header_sibar();
   		$id=$_GET['id'];
        if(!$id){
        	return;
        }
        $product = M('Product');
        $cate = M('Bigcategory');
        $soft=$product->find($id);

        $soft['introduce']=stripcslashes($soft['introduce']);
        $c_data = $cate->field('big_category_name,id,statice_name')->find($soft['pid']);
        $soft['bname'] = $c_data['big_category_name'];
        $soft['statice_name']=$c_data['statice_name'];
        $soft['bigid'] = $c_data['id'];
   		if(file_exists("./".$soft['bname'].'-software.html')){
    		$soft['file_exit'] = 1;
    	}
   		if($soft['one_download_link'] == 0){
        	$dl = M('ProductDownloadLink');
        	$pdl = $dl->where('pid='.$soft['id'])->order('sort asc')->select();
        	$soft['duoLink'] = $pdl;
        }
		if($soft['two_id']){
			$soft2=$product->find($soft['two_id']);
			$c_data = $cate->field('big_category_name,id')->find($soft2['pid']);
			$soft2['bname'] = $c_data['big_category_name'];
			if($soft2['one_download_link'] == 0){
				$dl = M('ProductDownloadLink');
	        	$pdl = $dl->where('pid='.$soft2['id'])->order('sort asc')->select();
	        	$soft2['duoLink'] = $pdl;
			}
		}
		$this->assign('soft2',$soft2);
        $this->assign('soft',$soft);
        $this->tkd($soft['product_title'],$soft['product_keyword'],$soft['introduce']);
        $this->assign('img_show',1);
   	 	$this->display();
   }
//	public function classify(){
//		Load('extend');
//        $this->header_sibar();
//		$id=$_GET['id'];
//        if(!$id){
//        	return;
//        }
//        $product=M('Product');
//
//        $soft=$product->where('pid='.$id)->order( 'add_time')->select();
//        $this->assign('soft',$soft);
//
//        $plate=M('Plate');
//        $pdata=$plate->find($id);
//        $this->assign('second_name',$pdata['plate_name']);
//        $this->assign('keyword',$pdata['plate_keyword']);
//        if($pdata['plate_title']){
//        	$this->assign('title',$pdata['plate_title']);
//        }
//        $cate=M('Bigcategory');
//        $cate=$cate->find($pdata['pid']);
//        $this->assign('first_name',$cate['big_category_name']);
//        $this->assign('description',$this->html($pdata['plate_description']));
//        $this->display();
//    }
	public function article(){
		Load('extend');
        $this->header_sibar();
        $id=$_GET['id'];
        if(!$id){
        	return;
        }
        $article=M('Article');
        $ac = M('ArticleClassify');
        $adata=$article->find($id);
        $this->tkd($adata['web_title'],$adata['keyword'],$adata['introduce']);

        $cate = M('Bigcategory');
        $c_data = $cate->field('big_category_name,id')->find($adata['pid']);
        $acMsg = $ac->field('id,classify_name,statice_name as acsn,is_statice as acis')->where('id='.$adata['cid'])->find();
        $adata['bname'] = $c_data['big_category_name'];
        $adata['bigid'] = $c_data['id'];
        $adata['acid'] = $acMsg['id'];
        $adata['acsn'] = $acMsg['acsn'];
        $adata['acis'] = $acMsg['acis'];
        $adata['c_name']=str_replace('復元','',$acMsg['classify_name']);
		if(file_exists("./".$adata['bname'].'-software.html')){
    		$adata['file_exit'] = 1;
    	}
        $this->assign('adata',$adata);
//        if(!empty($adata['search_word'])){
//        	$relArt = $article->where("title like '%".$adata['search_word']."%' and pid=".$adata['pid'])->select();
//        }else{
//        	$relArt = $article->where("pid=".$adata['pid'])->select();
//        }
//        $this->assign('relArt',$relArt);
		//$art = $this->getArticle('asort=1','*','10','sort');
		$show_art = $this->getArticle('status=1 and show_in_article=1 and cid='.$adata['cid'],'*','10','asort');
    	//$this->assign('art',$art);
    	$this->assign('show_art',$show_art);
    	$this->assign('img_show',1);
        $this->display();
    }
	public function howto(){
		Load('extend');
		$this->header_sibar();
        $b=M('Bigcategory');
		$acid=$_GET['id'];
		$pid = $_GET['pid'];
        if(!$acid || !$pid){
        	return;
        }
      
        $this->assign('sebg',1);
        $this->assign('pid',$pid);
        $a = M('Article');
        $p = M('Product');
		$ac = M('ArticleClassify');
	 	$dl = M('ProductDownloadLink');
		$acdata = $ac->where('pid='.$pid)->order('asort asc')->select();
	
		$msg = $ac->find($acid);
		$this->tkd($msg['seo_title'],$msg['seo_keywords'],$msg['seo_description']);
		$bMsg =  $b->find($pid);
		//$hdata = $product->field('think_product.*,b.big_category_name as bname')->where('think_product.is_hot=1')->join('think_bigcategory b on b.id = think_product.pid')->limit(2)->select();
		if($msg['pro_id']){
			$pMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['pro_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
			$pMsg = $pMsg[0];
			if($pMsg['one_download_link'] == 0){
	        	$pdl = $dl->where('pid='.$pMsg['id'])->order('sort asc')->select();
	        	$pMsg['duoLink'] = $pdl;
			}
	    }else{
	    	$pMsg = '';
	    }
        if($msg['psec_id']){
			$relativeMsg = $p->field('think_product.*,b.big_category_name as bname')->where('think_product.id='.$msg['psec_id'])->join('think_bigcategory b on b.id = think_product.pid')->limit(1)->select();
	        $relativeMsg = $relativeMsg[0];
		 	if($relativeMsg['one_download_link'] == 0){
	        	$pdl = $dl->where('pid='.$relativeMsg['id'])->select();
	        	$relativeMsg['duoLink'] = $pdl;
	        }
        }else{
        	$relativeMsg = '';
        }
//		foreach($acdata as $k => $v){
			$awdata = $a->where('status=1 and cid='.$acid.' and pid='.$pid.' and pc=1')->select();
			$amdata = $a->where('status=1 and cid='.$acid.' and pid='.$pid.' and pc=2')->select();
//		}
		$this->assign('bMsg',$bMsg);
		$this->assign('rMsg',$relativeMsg);
		$this->assign('pMsg',$pMsg);
		$this->assign('sMsg',$msg);
        $this->assign('acMsg',$acdata);
        $this->assign('awMsg',$awdata);
         $this->assign('amMsg',$amdata);
        $this->assign('acid',$acid);
       $this->display();
    }

    private function tkd($title='',$keyword='',$des=''){
    	if($title){
    		$this->assign('title',$title);
    	}
    	if($keyword){
    		$this->assign('keyword',$keyword);
    	}
    	if($des){
    		$this->assign('description',$this->html($des));
    	}
    }
   //过滤html标硡
	private function html($str){
		$pattern = "/(<strong>|<\/strong>|<br>|<br \/>)/i";
		return preg_replace($pattern,"",$str);
	}
	public function buy_num(){
		$id = $_POST['id'];
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('buy_num',$pdata['buy_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'buy_num'=>1,'time'=>$date))->add();
		}
//		$pdata = $product->field('buy_link,buy_num')->find($id);
//		$product->where('id='.$id)->setField('buy_num',$pdata['buy_num']+1);
//		$this->assign('buy_link',$pdata['buy_link']);
//		header("Location:".$pdata['buy_link']);
//		$this->display();
	}
	public function downloadNum(){
		$id = $_POST['id'];
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();
		}
//		$pdata = $product->field('download_link,download_num')->find($id);
//		$product->where('id='.$id)->setField('download_num',$pdata['download_num']+1);
//		echo $pdata['download_link'];

	}

	public function download()
	{
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
//		$product = M('Product');
		$dy = M('DownloadBuy');
		$pdl = M('ProductDownloadLink');
		$p = $pdl->where('id='.$id)->find();
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$p['pid'].' and time="'.$date.'" and dlid='.$id)->find()){
			$down_num = $pdata['down_num']+1;
			$dldown_num = $pdata['dldown_num']+1;
			$dy->where('pid='.$p['pid'].' and time="'.$date.'" and dlid='.$id)->setField(array('down_num','dldown_num'),array($down_num,$dldown_num));
		}else{
			$dy->data(array('pid'=>$p['pid'],'down_num'=>1,'time'=>$date,'dlid'=>$id,'dldown_num'=>1))->add();
		}
//		$pdata = $product->field('download_link,download_num')->find($p['pid']);
//		header("Location:".$p['url']);
		$url = str_replace(array("\n", "\r"), '', $p['url']);
//		redirect($url);
		$this->assign('url',$url);
		$this->display('downloading');
	}

	public function buy()
	{
		$id = $_GET['id'];
		if(empty($id))
		{
			return;
		}
		$product = M('Product');
		$pdata = $product->find($id);
		$this->assign('product',$pdata);
		$this->display();
	}
 	public function aboutUs(){
       $this->common();
       $this->assign('title','会社について– Transfer-iPhone-Recovery.jp');
       $this->assign('keyword',' iPhone/iPad/iPodデータ、転送ソフト、復元ソフト、会社情報');
       $this->assign('description','Transfer-iPhone-Recovery.jpはプロiPhoneデータの転送・復元するソフトをご提供します。');
       $this->display();
    }
	public function contactUs(){
       $this->common();
       $this->assign('title','ご連絡先– Transfer-iPhone-Recovery.jp');
       $this->assign('keyword','iPhone/iPad/iPodデータ、転送ソフト、復元ソフト、ご連絡先');
       $this->assign('description',"iPhone/iPad/iPod転送・復元及びパソコンデータの復元ソフトなど当社製品についてどうぞお問い合わせてください。");
       $this->display();
    }
	public function privacyPolicy(){
       $this->common();
        $this->assign('title','プライバシーポリシー– Transfer-iPhone-Recovery.jp');
       $this->assign('keyword','iPhone/iPad/iPodデータ、転送ソフト、復元ソフト、プライバシーポリシー');
       $this->assign('description','ファイルの復元・転送ソフト製品をご提供しているTransfer-iPhone-Recovery.jpのプライバシーポリシー。');
       $this->display();
    }
	public function termsConditions(){
       $this->common();
        $this->assign('title','Terms & Conditions - Transfer-iPhone-Recovery.jp');
       $this->assign('keyword','terms and conditions');
       $this->assign('description','Terms & Conditions of Transfer-iPhone-Recovery.jp, iPhone Transfer and Recovery software.');
       $this->display();
    }
	public function links(){
       $this->common();
        $this->assign('title','リンクポリシー– Transfer-iPhone-Recovery.jp');
       $this->assign('keyword','iPhone/iPad/iPodデータ、転送ソフト、復元ソフト、リンクポリシー');
       $this->assign('description','Transfer-iPhone-Recovery.jpはリンクフリーですから、リンク方法に沿い、相互リンクも可能です。');
       $this->display();
    }
    private function common()
    {
    	 header("Content-Type:text/html; charset=utf-8");
        Load('extend');
        $this->header_sibar();
        $product=M('Product');
        $pdata=$product->where('status=1 and is_popular=1')->limit(4)->select();
        $this->assign('product',$pdata);
        $hdata=$product->where('status=1 and is_hot=1')->limit(2)->select();
        $this->assign('hproduct',$hdata);
        $newsoft=$product->where("status=1")->order('add_time desc')->limit(12)->select();
        $this->assign('newsoft',$newsoft);
        $this->assign('flag',1);

    }
	public function purchasing(){
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('buy_num',$pdata['buy_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'buy_num'=>1,'time'=>$date))->add();
		}
		$pdata = $product->field('buy_link,buy_num')->find($id);
//		$product->where('id='.$id)->setField('buy_num',$pdata['buy_num']+1);
//		$this->assign('buy_link',$pdata['buy_link']);
redirect($pdata['buy_link'],0,'loading');
//		header("Location:".$pdata['buy_link']);
//		$this->display();
	}
	public function downloading(){
		$id = $_GET['id'];
		if(empty($id)){
			return false;
		}
		$product = M('Product');
		$dy = M('DownloadBuy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();
		}
		$pdata = $product->field('download_link,download_num')->find($id);
		$url = str_replace(array("\n", "\r"), '', $pdata['download_link']);
//		redirect($url);
$this->assign('url',$url);
$this->display();
//		echo "<script>window.open('".$url."','new','width=200,height=200')</script>";
//		$product->where('id='.$id)->setField('download_num',$pdata['download_num']+1);
//		echo $pdata['download_link'];

	}

	public function search()
	{
		Load('extend');
        $this->header_sibar();
		$keywords=$_GET['tag'];
        if(!$keywords){
        	return;
        }
        $article=M('Article');
        import("ORG.Util.Page"); //  导入分页�?
		$count = $article->where("title like '%".$keywords."%'")->count(); //  查衡满足要求的�?�衡录数
        $Page = new Page($count,10);

         $Page->setConfig('header', 'records');
        $Page->setConfig('prev', '< prev');
        $Page->setConfig('next', 'next >');
        $Page->setConfig('first', 'first');
        $Page->setConfig('last', 'last');
//        $Page->setConfig('theme', '%totalRow% %header% %nowPage%/%totalPage% pages %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
        $Page->setConfig('theme', '%totalRow% %header% %nowPage%/%totalPage% pages %upPage% %first% %linkPage% %downPage% %end%');
        $show  = $Page->show(); //  分页显示输出

        $soft=$article->where("title like '%".$keywords."%'")->order( 'add_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('keywords',$keywords);
        $this->assign('article',$soft);
        $this->assign('page',$show); //  赋�?�分页输�?

        $this->display();
	}

	public function search_result()
	{
        $this->header_sibar();

        $this->display();
	}

	public function tuji(){
		$id = $_POST['id'];
		if(empty($id)){
			return false;
		}
		$product = M('product');
		$dy = M('download_buy');
		$date = date("Y-m-d");
		if($pdata = $dy->where('pid='.$id.' and time="'.$date.'"')->find()){
			$dy->where('pid='.$id.' and time="'.$date.'"')->setField('down_num',$pdata['down_num']+1);
		}else{
			$dy->data(array('pid'=>$id,'down_num'=>1,'time'=>$date))->add();
		}



echo $id;

	}
	//2013年6月25日 首页box2是否显示
	public function showbox2(){
		$product=M('product');
		if($date=$product->where('status=1 and show_home=1')->find()){
			$show=1;
			$this->assign('show',$show);
		}else{
			$show=0;
			$this->assign('show',$show);
		}

	}
	//读出box2中要的数据
	public function box2(){

		$cate = M('Bigcategory');
		$c_data = $cate->field('id,big_category_name as bname')->select();

		foreach($c_data as $k => $v){
			$bid=$v['id'];

			$product = M('product');
			$c_data[$k]['pro'] = $product->field('id,product_name,pc,is_statice,statice_name,out_link,home_img')->where('show_home = 1 and pid = '.$bid)->limit(4)->select();

		}


			$this->assign('showpro',$c_data);
	}

	public function show404(){
		Load('extend');
        $this->header_sibar();
		$title = 'ページが見つかりません';
		$keyword='エラーページ、ページが見つかりません、404';
		$description='申し訳ございません。このページは削除された、またはURLが変更されました。';
		$this->tkd($title,$keyword,$description);
		$this->display();
	}
	
	public function question(){
		$question = M('Question') ;
		$_POST['add_time']=time();
		if($_POST['email'] !='' && $_POST['title'] !='' && $_POST['content'] !=''){
			if($question->add($_POST))
				{
					echo "<script>alert('正常に送信しました。');location.href='".__ROOT__."/contact-us.html';</script>";
				}else{
					echo "<script>alert('エラーが発生しました。');history.back();</script>";
				}
			
		}else{
			echo "<script>alert('エラーが発生しました。');history.back();</script>";
		}
	}
		
	
}
?>